package com.hqd.ch03.v50.boot.logging;

import java.util.List;
import java.util.function.Function;

class DelegatingLoggingSystemFactory implements LoggingSystemFactory {

    private final Function<ClassLoader, List<LoggingSystemFactory>> delegates;

    DelegatingLoggingSystemFactory(Function<ClassLoader, List<LoggingSystemFactory>> delegates) {
        this.delegates = delegates;
    }

    @Override
    public LoggingSystem getLoggingSystem(ClassLoader classLoader) {
        List<LoggingSystemFactory> delegates = (this.delegates != null) ? this.delegates.apply(classLoader) : null;
        if (delegates != null) {
            for (LoggingSystemFactory delegate : delegates) {
                LoggingSystem loggingSystem = delegate.getLoggingSystem(classLoader);
                if (loggingSystem != null) {
                    return loggingSystem;
                }
            }
        }
        return null;
    }

}
